package top.kindaries.device.provider.impl;

import top.kindaries.device.Device;
import top.kindaries.device.provider.IDeviceProvider;
import top.kindaries.model.Prop;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class DefaultProvider implements IDeviceProvider {

    private static Map<String, Device> devs = null;

    static {
        //读取监控设备配置信息
        Prop prop;
        try {
            prop = new Prop("config/monitor.properties");
        } catch (Exception e) {
            //加载失败时，抛出异常
            throw new RuntimeException("monitor.properties不存在，无法加载设备信息");
        }
        //读取配置信息
        devs = new HashMap<String, Device>();
        String codes = prop.get("monitor.codes");
        for (String code : codes.split(",")) {
            String title = prop.get(String.format("monitor.%s.title", code));
            String url = prop.get(String.format("monitor.%s.stream_url", code));
            devs.put(code, new Device(code, title, url));
        }
    }

    public boolean isContain(String code) {
        return devs.containsKey(code);
    }

    public Device get(String code) {
        return devs.get(code);
    }

    public Collection<Device> getAll() {
        return devs.values();
    }

}
